package com.ufo.store.management.widget.view;

/**
 * Created by taoli on 2018/2/28.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.ufo.store.management.R;

public class PhotoFrameImageView extends ImageView {
    private final Context context;
    public PhotoFrameImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable != null) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b = getCircleBitmap(bitmap);
            Rect dst = new Rect(0, 0, getWidth(), getHeight());
            Rect src = new Rect(0, 0, b.getWidth(), b.getHeight());
            canvas.drawBitmap(b, src, dst, new Paint());
        } else {
            super.onDraw(canvas);
        }
    }

    private Bitmap getCircleBitmap(Bitmap bitDst) {
        //此处添加相框，将相框叠加在图片上
        Bitmap bitmap = Bitmap.createBitmap(bitDst.getWidth(), bitDst.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(), R.drawable.device_pic_8_16_nvr);
        Rect src = new Rect(0, 0, bitmapSrc.getWidth(), bitmapSrc.getHeight());
        Rect dst = new Rect(0, 0, bitDst.getWidth(), bitDst.getHeight());
        canvas.drawBitmap(bitDst, dst, dst, null);
        canvas.drawBitmap(bitmapSrc, src, dst, null);
        return bitmap;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}